#include <QApplication>
#include "MainWindow.h"
#include <QDebug>
#include <CryptoAgent.h>
#include <QTextCodec>
#include <openssl/evp.h>
#include "crypt/CryptAES.h"
#include "crypt/CryptDES.h"
#include "crypt/CryptRC4.h"
#include <iostream>

void testCryptoAgent();
void testEVP();

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	a.setApplicationVersion(APP_VERSION);

	QTranslator qtTranslator;
	qtTranslator.load("Memorizer.qm");
	a.installTranslator(&qtTranslator);

    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("CP-1251"));

	MainWindow w;
	w.show();

	return a.exec();

	//testCryptoAgent();

	//testEVP();
}


void testCryptoAgent()
{
    CryptoAgent cr;
	cr.setPassword(CipherType::RC4,"str");

    QString pass = "razorl1s s s";
    QString data = "341dfqer23sd dsd ds d sd sd d 123 123 123 ";

	for(int i=1;i<pass.length()-1;i++){
		QString ps = pass.left(i);
		cr.setPassword(CipherType::RC4,ps);
        for(int j=1;j<data.length()-1;j++){
            QString dt = data.left(j);
			QByteArray tmp1 = cr.encrypt(dt.toAscii());
			QByteArray tmp2 = cr.decrypt(tmp1);
            if(tmp2 != dt){
               // qDebug()<<QString("fail. Pass=%1[%2], Data=%3[%4], Data2=%5").arg(ps).arg(i).arg(dt).arg(j).arg(tmp);
				qDebug()<<QString("fail. Data1=%1, Data2=%2, Data3=%3").arg(QString(dt)).arg(QString(tmp1)).arg(QString(tmp2));
            }
        }
	}

    qDebug()<<"done";
}

void testEVP(){
	 QByteArray data = "341dfqer23sd dsd ds d sd sd d 123 123 123 ";

	 CryptAES aes;
	 QByteArray arr("1234567890123456");

	 aes.setPass(arr,16);

	 for(int j=1;j<data.length()-1;j++){
		 QByteArray dt = data.left(j);
		 QByteArray tmp1 = aes.encrypt(dt,true);
		 QByteArray tmp2 = aes.encrypt(tmp1,false);
		 if(tmp2 != dt){
			 std::cout<<"fail. Data1="<<dt.data()<<", Data2="<<tmp1.data()<<", Data3="<<tmp2.data()<<endl;
		 }
	 }
	 std::cout<<"done";
	 qDebug()<<"done";
}
